package com.yunfeng.supercall.activity;

import java.util.Timer;
import java.util.TimerTask;

import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.lin.activity.EntrepriseRegisterStep2Activity;
import com.lin.activity.PersonalRegisterStep2Activity;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

public class RegStepFirstEntrepriseActivity extends BaseActivity {

	private String mParams;
	private Button mButtonCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reg_entreprise_step1);
		mButtonCode = findView(R.id.code);
		mParams = getIntent().getStringExtra("data");
		mEditTextPhone = (EditText) findViewById(R.id.phone);
frameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		
		frameLayoutBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	public void next(View v) {
		String code = getTextViewText(R.id.et_check_code);
		if (TextUtils.isEmpty(code)) {
			showToast(R.string.input_valid_code);
			return;
		}
		AjaxParams mAjaxParams = new AjaxParams();
		mAjaxParams.put("i_phone", getTextViewText(R.id.phone));
		mAjaxParams.put("i_code", code);
		showProgressDialog(R.string.virefing);
		mHttpClient.post(FUNCTION_CHECK_CODE, mAjaxParams,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showToast(R.string.incorrect_number);
						cancelProgressDialog();
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						cancelProgressDialog();
//							JSONObject json = new JSONObject(mParams);
//							json.put("i_phone", getTextViewText(R.id.phone));
							
							Intent intent = new Intent(RegStepFirstEntrepriseActivity.this,
									EntrepriseRegisterStep2Activity.class);
						intent.putExtra("phone", mEditTextPhone.getText().toString());
							startActivity(intent);
							

					}
				});
		// VerificationCode.doVerificationCode(arg0, arg1, arg2, arg3, arg4,
		// arg5)
		// try {
		// JSONObject json = new JSONObject(mParams);
		// json.put("i_phone", getTextViewText(R.id.phone));
		// startActivity(new Intent(this, RegStep3Activity.class).putExtra(
		// "data", json.toString()));
		// } catch (JSONException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	public String checkPhone() {
		return "1";
	}

	public void getCode(View v) {
		final String phone = getTextViewText(R.id.phone);
		if (AppContext.isMobileNO(phone)) {
			AjaxParams mAjaxParams = new AjaxParams();
			mAjaxParams.put("i_phone", phone);
			mAjaxParams.put("i_ischeckphone", checkPhone());
			showProgressDialog(R.string.getting_valid_code);
			mHttpClient.post(FUNCTION_GET_PHONE_CHECK_CODE, mAjaxParams,
					new MyAjaxCallBack() {

						@Override
						public void onReceiveError(String msg, String errorCode) {
							// TODO Auto-generated method stub
							showToast(msg);
							cancelProgressDialog();
						}

						@Override
						public void onReceiveData(String data, String msg) {
							// TODO Auto-generated method stub
							startCountDown();
							cancelProgressDialog();
						}
					});
		} else {
			showToast(R.string.input_correct_phone_number);
		}
	}

	private Handler mHandler = new Handler();
	private int mSeconds = 120;
	private Timer mTimer;
	private TimerTask mTimerTask;
	private EditText mEditTextPhone;
	private FrameLayout frameLayoutBack;

	private void startCountDown() {
		stopCountDown();
		mButtonCode.setEnabled(false);
		mSeconds = 120;
		mTimer = new Timer();
		mTimerTask = new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mSeconds--;
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						if (mSeconds == 0) {
							stopCountDown();
							mButtonCode.setEnabled(true);
							mButtonCode.setText(R.string.get_valid_code);
						} else {
							mButtonCode
									.setText(getResources()
											.getQuantityString(
													R.plurals.get_valid_code_after_seconds,
													mSeconds, mSeconds));
						}
					}
				});
			}
		};
		mTimer.schedule(mTimerTask, 0, 1000);
	}

	private void stopCountDown() {
		if (mTimer != null) {
			mTimer.cancel();
		}
		if (mTimerTask != null) {
			mTimerTask.cancel();
		}
	}
}
